mc.game.leader_list = function(list){
	this.list = list.sort(mc.game.sort_by_entry);
}
mc.game.leader_list.prototype.find_source = function(){
	if(this.source == undefined){
		var reference = mc.game.get_proto("leaders");
		for(var i = 0; i < this.list.length; i++){
			var proto = reference[this.list[i].proto_id];
			if(proto.type_i == 1){
				this.source = i;
				break;
			}
		}
	}
	if(this.source == undefined){
		alert("bad data, cannot found source");
	}
	return this.list[this.source];
}
mc.game.leader_list.prototype.leaders = function(){
	var reference = mc.game.get_proto("leaders");
	var result = [];
	for(var i = 0; i< this.list.length; i++){
		var o = {
			p: reference[this.list[i].proto_id],
			ct: this.list[i].current_task,
			e: this.list[i].entry
		};
		result.push(o);
	}
	return {list: result};
}

mc.game.leader_list.prototype.personal = function(entry,all){
	var source = this.find_source();
	var s_entry = source.entry;
	source = source.units;
	
	var target = this.leader(entry).units;
	var source_index = {};
	
	for(var i = 0; i < source.length; i++){
		source_index[source[i].proto_id] = source[i];
	}
	var result = [];
	var reference = mc.game.get_proto("units");
	for(var i = 0; i < target.length; i++){
		var t = target[i];
		var s = source_index[t.proto_id];
		delete source_index[t.proto_id];
		var o = {
			proto: reference[t.proto_id],
			count: t.count,
			source: s ? s.count : 0
		};
		if(all || o.count > 0 || o.source > 0){
			result.push(o);
		}
	}
	for(var p in source_index){
		var o = {
			proto: reference[p],
			count: 0,
			source: source_index[p].count
		};
		if(all || o.source > 0){
			result.push(o);
		}
	}
	return {
		list: result,
		source: s_entry,
		target: entry
	};
}
mc.game.leader_list.prototype.inventory = function(entry,all){
	var source = this.find_source();
	var s_entry = source.entry;
	source = source.items;
	
	var target = this.leader(entry).items;

	var has = [];
	var can = [];
	var reference = mc.game.get_proto("items");
	for(var i = 0; i < target.length; i++){
		var t = target[i];
		var o = {
			proto: reference[t.proto_id],
			entry: t.entry
		};
		has.push(o);
	}
	if(!all){
		for(var i = 0; i < source.length; i++){
			var s = source[i];
			var o = {
				proto: reference[s.proto_id],
				entry: s.entry
			};
			can.push(o);
		}
	}
	return {
		has: has,
		can: can,
		source: s_entry,
		target: entry
	};
}

mc.game.leader_list.prototype.ask_unit = function(entry){
	if(mc.leader_list.has_units(entry)){
		return null;
	}else{
		return mc.request(
				"game",
				{cmd: "units.load",entry: entry},
				function(data){
					mc.leader_list.set_units(this.entry, data);
				},
				{entry: entry}
			);
	}
}
mc.game.leader_list.prototype.has_units = function(entry){
	var l = this.leader(entry);
	return l && l.units;
}
mc.game.leader_list.prototype.set_units = function(entry,value){
	var l = this.leader(entry);
	if(l){
		l.units = value.sort(mc.game.sort_by_proto);
	}
}
mc.game.leader_list.prototype.units = function(entry){
	var list = this.leader(entry).units;
	var reference = mc.game.get_proto("units");
	var result = [];
	for(var i = 0; i< list.length; i++){
		var o = {
			p: reference[list[i].proto_id],
			c: list[i].count,
			e: list[i].entry
		};
		if(o.c > 0){
			result.push(o);
		}
	}
	return {list: result};
}

mc.game.leader_list.prototype.ask_item = function(entry){
	if(mc.leader_list.has_items(entry)){
		return null;
	}else{
		return mc.request(
				"game",
				{cmd: "items.load",entry: entry},
				function(data){
					mc.leader_list.set_items(this.entry, data);
				},
				{entry: entry}
			);
	}
}
mc.game.leader_list.prototype.has_items = function(entry){
	var l = this.leader(entry);
	return l && l.items;
}
mc.game.leader_list.prototype.set_items = function(entry,value){
	var l = this.leader(entry);
	if(l){
		l.items = value.sort(mc.game.sort_by_proto);
	}
}
mc.game.leader_list.prototype.items = function(entry){
	var list = this.leader(entry).items;
	var reference = mc.game.get_proto("items");
	var result = [];
	for(var i = 0; i< list.length; i++){
		var o = {
			p: reference[list[i].proto_id],
			e: list[i].entry
		};
		result.push(o);
	}
	return {list: result};
}


mc.game.leader_list.prototype.leader = function(entry){
	for(var i = 0; i < this.list.length; i++){
		if(this.list[i].entry == entry){
			return this.list[i];
		}
	}
	return false;
}
mc.game.leader_list.prototype.task = function(entry){
	return this.leader(entry).cto;
}
mc.game.leader_list.prototype.clear_task = function(entry){
	var l = this.leader(entry);
	delete l.cto;
	delete l.current_task;
}
mc.game.leader_list.prototype.has_task = function(entry){
	var leader = this.leader(entry);
	if(leader.current_task){
		return leader.cto;
	}else{
		return true;
	}
}
mc.game.leader_list.prototype.set_task = function(entry,data){
	var leader = this.leader(entry);
	if(data && data.entry){
		leader.current_task = data.entry;
		data.proto = mc.game.get_proto("tasks_common")[data.proto_id];
		data.type = mc.game.get_proto("task_types")[data.proto.type];
		leader.cto = data;
	}else{
		leader.current_task = 0;
	}
}
mc.game.leader_list.prototype.ask_task = function(entry){
	if(mc.leader_list.has_task(entry)){
		return null;
	}else{
		return mc.request(
				"game",
				{cmd: "task.current",entry: entry},
				function(data){
					mc.leader_list.set_task(this.entry, data);
				},
				{entry: entry}
			);
	}
}
